/*
 * @Author: DUST
 * @Date: 2023-05-31 10:28:52
 * @LastEditors: DUST
 * @LastEditTime: 2023-06-09 13:40:22
 * @FilePath: \vite-project\src\store\user.ts
 * @Description: 
 * undefined
 */

import { TokenData, UserInfo } from "@/interface";
import { defineStore } from "pinia";
import store from ".";

interface State {
  userinfo: UserInfo | null | undefined,
  tokenInfo: TokenData | null | undefined
}

const userStore = defineStore("user", {
  state: (): State => ({
    userinfo: null,
    tokenInfo: null
  }),
  persist: {
    enabled: true // true 表示开启持久化保存
  },
  // 相当于vue的compute，在getters中使用了this则必须手动指定返回值类型，否则类型推导不出来
  getters: {},
  // 相当于vue的method，在actions中不能使用箭头函数，因为箭头函数绑定外部this
  actions: {
    setUserInfo(userinfo: UserInfo) {
      this.userinfo = userinfo;
    },
    setTokenInfo(tokenInfo: TokenData) {
      this.tokenInfo = tokenInfo;
    }
  }
})
 export default userStore(store);